﻿using System;
using System.Net;
using System.Linq;
using System.Threading;
using System.Collections.Generic;
using System.Collections.ObjectModel;
///
using ScheduleMVVM.Model.Miscellaneous;
using ScheduleMVVM.Model.PersistentFramework;


namespace ScheduleMVVM.Model.RuntimeFramework
{
    /// <summary>
    /// Contains runtime application model-related data
    /// </summary>
    public class RuntimeContext
    {
        // Binded collections
        /// <summary>
        /// Contains all stations for current Search engine
        /// </summary>
        public StationEntry[] Stations
        {
            get
            {
                return stationsXML.Value;
            }
        }
        private List<String> _departments;
        /// <summary>
        /// Departments yielded from Stations file
        /// </summary>
        public String[] Departments
        {
            get
            {
                if (_departments == null)
                {
                    _departments = new List<string>();
                    GetDepartments();
                }
                return _departments.ToArray();
            }
        }
        private void GetDepartments()
        {
            foreach (var i in Stations)
            {
                if (!_departments.Contains(i.Department)) _departments.Add(i.Department);
            }
        }
        /// <summary>
        /// User favourites
        /// </summary>
        public List<RouteEntry> Favourites
        {
            get
            {
                return favouritesXML.Value;
            }
        }
        // XML files - real storage
        protected SyncableXml<StationEntry[]> stationsXML;
        protected SyncableXml<List<RouteEntry>> favouritesXML;

        public RuntimeContext()
        {
#if DOTNET_LIB
            stationsXML = new SyncableXml<StationEntry[]>("stations_tuturuengine.xml");
#else
            stationsXML = new SyncableXml<StationEntry[]>(System.Windows.Application.GetResourceStream(new Uri("Prerequisites/" + Settings.Current.CurrentEngine.StationsStore, UriKind.Relative)).Stream, true);
            favouritesXML = new SyncableXml<List<RouteEntry>>(Settings.Current.CurrentEngine.FavouritesStore);
#endif
            if (!stationsXML.DataPresent) throw new System.IO.FileNotFoundException("Stations file not found");
        }
        /// <summary>
        /// Returns favourite entry corresponding to 'from' and 'to' stations
        /// </summary>
        /// <param name="From">Departure station</param>
        /// <param name="To">Arrival station</param>
        /// <returns></returns>
        public RouteEntry GetFavouriteEntry(StationEntry From, StationEntry To)
        {
            return Favourites.First<RouteEntry>(x => x.startStation == From && x.endStation == To);
        }
        public RouteEntry GetFavouriteEntry(Int32 FromId, Int32 ToId)
        {
            return Favourites.First<RouteEntry>(x => x.startStation.id == FromId && x.endStation.id == ToId);
        }
    }
   
}

